package com.coocaa.ops.admin.v1.homepage.convert;

import com.coocaa.ops.common.core.base.enums.ResourceTypeEnum;
import com.coocaa.ops.common.core.biz.dmp.entity.DmpRegistryRlsInfoEntity;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageModuleRlsEntity;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/16 15:59
 */
@UtilityClass
public class HomeSaveConverter {
    /**
     * 保存前处理
     *
     * @param entity
     */
    public static void handleBeforeSave(HomepageInfoEntity entity) {
        //是否有dmp内容设置
        handleSetHasDmp(entity);
    }

    /**
     * 封装首页模块关联信息
     * @param entity
     * @param homeModuleContentConfigInfo
     * @return
     */
    public static HomepageModuleRlsEntity getHomepageModuleRlsEntity(HomepageInfoEntity entity, HomeModuleContentConfigInfo homeModuleContentConfigInfo){
        HomepageModuleRlsEntity saveRlsEntity = new HomepageModuleRlsEntity();
        saveRlsEntity.setHomepageId(entity.getId());
        saveRlsEntity.setHomepageVersion(entity.getCurrentVersion());
        saveRlsEntity.setModuleId(homeModuleContentConfigInfo.getId());
        saveRlsEntity.setModuleIsFocus(homeModuleContentConfigInfo.getModuleIsFocus());
        saveRlsEntity.setCreator(entity.getModifier());
        saveRlsEntity.setModifier(entity.getModifier());
        return saveRlsEntity;
    }

    public static DmpRegistryRlsInfoEntity getDmpRegistryRlsInfoEntity(HomepageInfoEntity entity, HomeModuleContentConfigInfo homeModuleContentConfigInfo){
        DmpRegistryRlsInfoEntity dmpRegistryRlsInfoEntity = homeModuleContentConfigInfo.getDmpRegistryInfo();
        dmpRegistryRlsInfoEntity.setParentContainerId(entity.getId());
        dmpRegistryRlsInfoEntity.setParentContainerVersion(entity.getCurrentVersion());
        dmpRegistryRlsInfoEntity.setParentContainerType(ResourceTypeEnum.HOMEPAGE.getValue());
        dmpRegistryRlsInfoEntity.setSubContainerId(homeModuleContentConfigInfo.getId());
        dmpRegistryRlsInfoEntity.setSubContainerType(ResourceTypeEnum.MODULE.getValue());
        dmpRegistryRlsInfoEntity.setCreator(entity.getModifier());
        dmpRegistryRlsInfoEntity.setModifier(entity.getModifier());
        return  dmpRegistryRlsInfoEntity;
    }



    private static void handleSetHasDmp(HomepageInfoEntity entity) {
        boolean isHasDmp = isHasDmp(entity);
        entity.setHasDmp( isHasDmp? 1 : 0);
    }


    private static  boolean isHasDmp(HomepageInfoEntity entity){
        List<HomeModuleContentConfigInfo> moduleList = entity.getModuleList();
        for (HomeModuleContentConfigInfo homeModuleContentConfigInfo : moduleList){
            if (Objects.equals(1, homeModuleContentConfigInfo.getModuleIsDmp())  && homeModuleContentConfigInfo.getDmpRegistryInfo() != null){
                return true;
            }
        }
        return false;
    }
}
